// src/api/food.js
import axios from 'axios'

/**
 * 获取所有食物
 * @returns {Promise<Food[]>}
 */
export function listFoods(foodType = '') {
    const params = {}
    if (foodType) params.foodType = foodType.trim()
    return axios.get('/food/list', { params })
}
/**
 * 根据 ID 获取单个食物
 * @param {number|string} id 食物 ID
 * @returns {Promise<Food>}
 */
export function getFood(id) {
    return axios.get(`/food/${id}`)
}

/**
 * 创建新的食物
 * @param {Object} food Food 对象，字段示例：{ name, calories, category, … }
 * @returns {Promise<void>}
 */
export function createFood(food) {
    return axios.post('/food/create', food)
}

/**
 * 更新已有的食物
 * @param {Object} food Food 对象，必须带 id，例如：{ id, name, calories, … }
 * @returns {Promise<void>}
 */
export function updateFood(food) {
    return axios.put('/food/update', food)
}

/**
 * 删除食物
 * @param {number|string} id 食物 ID
 * @returns {Promise<void>}
 */
export function deleteFood(id) {
    return axios.delete(`/food/delete/${id}`)
}

export function listFoodsChange({
                                    foodType = '',
                                    foodName = '',
                                    page = 1,
                                    size = 10
                                } = {}) {
    const params = {
        // 后端要求的参数名必须和接口一致
        page,
        size
    }
    if (foodType) params.foodType = foodType.trim()
    if (foodName) params.foodName = foodName.trim()

    return axios.get('/food/list-change', { params })
}